﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace ReportingManagement
{
    public partial class QueryParameters : Form
    {
        private const int BUTTON_COLUMN_INDEX = 2;
        private const int HEIGHT_PADDING = 2;
        private const int WIDTH_PADDING = 10;

        private static Dictionary<string, string> cachedParameters = new Dictionary<string, string>();

        private DataTable m_Parameters;

        public QueryParameters(DataTable parameters)
        {
            InitializeComponent();

            // Restore cached values
            foreach (string key in cachedParameters.Keys)
            {
                foreach (DataRow r in parameters.Rows)
                {
                    string parameterName = r[0].ToString();
                    if (key == parameterName)
                    {
                        r[2] = cachedParameters[key];
                        break;
                    }
                }
            }

            dgvParameters.AutoGenerateColumns = false;
            dgvParameters.DataSource = parameters;
            dgvParameters.Columns[0].DataPropertyName = parameters.Columns[0].ColumnName;
            dgvParameters.Columns[1].DataPropertyName = parameters.Columns[1].ColumnName;
            dgvParameters.Columns[2].DataPropertyName = parameters.Columns[2].ColumnName;
            
            int headerHeight = dgvParameters.ColumnHeadersHeight;
            int rowHeight = 0;
            foreach (DataGridViewRow r in dgvParameters.Rows) rowHeight += r.Height;
            int totalHeight = headerHeight + rowHeight + HEIGHT_PADDING;
            dgvParameters.Height = totalHeight;
            
            int totalWidth = 0;
            dgvParameters.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            foreach (DataGridViewColumn c in dgvParameters.Columns) totalWidth += c.Width;
            totalWidth += WIDTH_PADDING;
            dgvParameters.Width = totalWidth;

            this.m_Parameters = parameters;
        }

        private void dgvParameters_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == BUTTON_COLUMN_INDEX)
            {
                try
                {
                    DataGridViewRow row = dgvParameters.Rows[e.RowIndex];
                    string parameterName = row.Cells[0].Value.ToString();

                    DbType dbType = (DbType)row.Cells[1].Value;
                    string subtitle = String.Format("Enter a value of @{0}", parameterName);
                    string currentValue = (string)row.Cells[2].Value;

                    GetValue getValue = new GetValue(dbType, currentValue, subtitle);
                    getValue.Text = "Parameter Value";
                    getValue.SubTitle = subtitle;
                    
                    DialogResult response = getValue.ShowDialog(this);
                    if (response == System.Windows.Forms.DialogResult.OK)
                    {
                        object value = getValue.Value;
                        string sValue = value.ToString();
                        row.Cells[2].Value = sValue;
                        m_Parameters.Rows[e.RowIndex][2] = sValue;
                        cachedParameters[parameterName] = sValue;

                        int totalWidth = 0;
                        dgvParameters.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                        foreach (DataGridViewColumn c in dgvParameters.Columns) totalWidth += c.Width;
                        totalWidth += WIDTH_PADDING;
                        dgvParameters.Width = totalWidth;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        public DataTable Parameters
        {
            get
            {
                return this.m_Parameters;
            }
        }
    }
}
